﻿using System.Linq;
using ExtendedMvc.Ajax;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for SubmitTest and is intended
	///to contain all SubmitTest Unit Tests
	///</summary>
	[TestClass()]
	public class SubmitTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;
		private static ModelViewForm<Dummy> form;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
			form = new ModelViewForm<Dummy>(view.Html);
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for Submit Constructor
		///</summary>
		[TestMethod()]
		public void SubmitButton_WithContentOnly() {
			// Arrange
			var buttonContent = "Submit Text";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button onclick=""{1}"" type=""submit"">{0}</button>", buttonContent, onClickEvent);

			// Act
			var submit = form.SubmitButton(buttonContent);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Submit Constructor
		///</summary>
		[TestMethod()]
		public void SubmitButton_WithButtonId() {
			// Arrange
			var buttonContent = "Submit Text";
			var buttonId = "buttonId";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button id=""{2}"" onclick=""{1}"" type=""submit"">{0}</button>", buttonContent, onClickEvent, buttonId);

			// Act
			var submit = form.SubmitButton(buttonContent).Id(buttonId);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Submit Constructor
		///</summary>
		[TestMethod()]
		public void SubmitButton_Disabled() {
			// Arrange
			var buttonContent = "Submit Text";
			var expectedOutput = string.Format(@"<button disabled=""disabled"" type=""submit"">{0}</button>", buttonContent);

			// Act
			var submit = form.SubmitButton(buttonContent).Disabled();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Submit Constructor
		///</summary>
		[TestMethod()]
		public void SubmitButton_WithNoValidation() {
			// Arrange
			var buttonContent = "Submit Text";
			var onClickEvent = string.Format(JavaScriptHelper.Submit, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button onclick=""{1}"" type=""submit"">{0}</button>", buttonContent, onClickEvent);

			// Act
			var submit = form.SubmitButton(buttonContent).SkipValidation();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitLink Constructor
		///</summary>
		[TestMethod()]
		public void SubmitLink_ContentOnly() {
			// Arrange
			var informedContent = "SubmitLink Text";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<a href=""{1}"" type=""submit"">{0}</a>", informedContent, onClickEvent);

			// Act
			var submit = form.SubmitLink(informedContent);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitLink Constructor
		///</summary>
		[TestMethod()]
		public void SubmitLink_WithId() {
			// Arrange
			var informedContent = "SubmitLink Text";
			var imageId = "imageId";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<a href=""{2}"" id=""{1}"" type=""submit"">{0}</a>", informedContent, imageId, onClickEvent);

			// Act
			var submit = form.SubmitLink(informedContent).Id(imageId);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitLink Constructor
		///</summary>
		[TestMethod()]
		public void SubmitLink_Disabled() {
			// Arrange
			var informedContent = "SubmitLink Text";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<a disabled=""disabled"" href=""{1}"" type=""submit"">{0}</a>", informedContent, onClickEvent);

			// Act
			var submit = form.SubmitLink(informedContent).Disabled();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitLink Constructor
		///</summary>
		[TestMethod()]
		public void SubmitLink_WithNoValidation() {
			// Arrange
			var informedContent = "SubmitLink Text";
			var onClickEvent = string.Format(JavaScriptHelper.Submit, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<a href=""{1}"" type=""submit"">{0}</a>", informedContent, onClickEvent);

			// Act
			var submit = form.SubmitLink(informedContent).SkipValidation();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitImage Constructor
		///</summary>
		[TestMethod()]
		public void SubmitImage_ContentOnly() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<image onclick=""{1}"" src=""{0}"" type=""submit"" />", outputSource, onClickEvent);

			// Act
			var submit = form.SubmitImage(informedSource);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitImage Constructor
		///</summary>
		[TestMethod()]
		public void SubmitImage_WithId() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var imageId = "imageId";
			var onClickEvent = string.Format(JavaScriptHelper.SubmitWithValidation, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<image id=""{1}"" onclick=""{2}"" src=""{0}"" type=""submit"" />", outputSource, imageId, onClickEvent);

			// Act
			var submit = form.SubmitImage(informedSource).Id(imageId);
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitImage Constructor
		///</summary>
		[TestMethod()]
		public void SubmitImage_Disabled() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<image disabled=""disabled"" onclick=""{1}"" src=""{0}"" type=""submit"" />", outputSource, onClickEvent);

			// Act
			var submit = form.SubmitImage(informedSource).Disabled();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitImage Constructor
		///</summary>
		[TestMethod()]
		public void SubmitImage_Disabled_WithAlternateImage() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var informedDisabledSource = "~/images/dummy_off.jpg";
			var outputDisabledSource = "/images/dummy_off.jpg";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<image disabled=""disabled"" onclick=""{1}"" src=""{0}"" type=""submit"" />", outputDisabledSource, onClickEvent);

			// Act
			var submit = form.SubmitImage(informedSource).DisabledImage(informedDisabledSource).Disabled();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for SubmitImage Constructor
		///</summary>
		[TestMethod()]
		public void SubmitImage_WithNoValidation() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var onClickEvent = string.Format(JavaScriptHelper.Submit, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<image onclick=""{1}"" src=""{0}"" type=""submit"" />", outputSource, onClickEvent);

			// Act
			var submit = form.SubmitImage(informedSource).SkipValidation();
			var output = submit.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
